<?php
class AuctionsController extends AppController {

    var $name = 'Auctions';
    var $components = array('RequestHandler', 'Cookie');
    var $helpers = array('Html', 'Form', 'Time');
    
    function beforeFilter() {
#        $this->Auth->allow('index');
#        if ($this->RequestHandler->isAjax()) {
#            $this->Auth->allow('index');
#        }
        parent::beforeFilter();
#        $this->Auth->logoutRedirect = array('controller' => 'links', 'action' => 'index');
        // having a custom login function 
#        $this->Auth->autoRedirect = false;
    }

    
    function index() {
    
        $this->paginate = array(
            'limit' => 6,
            'order' => array('Product.id' => 'DESC'),
        );
        $products = $this->paginate();
        $this->loadModel('User');
        $token = $this->User->findById($this->Session->read('Auth.User.id'), array('contain' => false, 'fields' => 'token'));
#        debug($products);
        $this->set(compact('products', 'token'));
        
        if ($this->RequestHandler->isAjax()) {
            $this->render('/auctions/ajax_paging');
            return;
        }
    }
    
    function ajax_paging() {
        
        $this->paginate = array(
            'limit' => 6,
            'order' => array('Product.id' => 'DESC'),
        );
        $this->set('products', $this->paginate());
    }
    
    function winners() {
        $winners = $this->Auction->find('all', array('conditions' => array('Auction.time_left' => 0), 'order' => 'Auction.id DESC'));
        $this->set('winners', $winners);
        $this->set('title_for_layout', 'Winners');
#        debug($winners);
    }
    
#    function win() {
#        $this->autoRender = false;
#        $this->autoLayout = false;
#        
#        $this->Auction->Product->id = $_POST['product_id'];
#        $this->Auction->id = $_POST['auction_id'];
#        if ($this->Auction->saveField('currentBid', $_POST['winningbid']) && $this->Auction->saveField('currentBidder', $_POST['winner']) && $this->Auction->Product->saveField('isBid', 1)) {
#            echo "Done";
#        } else {
#            echo "False";
#        }
#    }
    
    function updateBid() {
        if ($this->RequestHandler->isAjax()) {
            $this->Auction->id = $_POST['id'];
            $this->autoRender = false;
            $this->autoLayout = false;           
            if ($this->Auction->saveField('currentBid', $_POST['bid']) && $this->Auction->saveField('currentBidder', $_POST['bidder'])) {
                echo 'Updated';
            } else {
                echo 'Error';
            }
        }
    }
    
    function updateTimer() {
        if ($this->RequestHandler->isAjax()) {
            $this->Auction->id = $_POST['id'];
            $this->autoRender = false;
            $this->autoLayout = false;           
            if ($this->Auction->saveField('time_left', $_POST['time_left'])) {
                echo 'Updated';
            } else {
                echo 'Error';
            }
        }
    }
    
}
?>
